package com.njrsrc.android.app;

import java.util.ArrayList;
import java.util.List;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.njrsrc.R;
import com.njrsrc.android.bean.HrLetterInfo;
import com.njrsrc.android.bean.UserInfo;
import com.njrsrc.android.library.vpi.BaseSampleActivity;
import com.njrsrc.android.tasks.HrLetterDetailTask;
import com.njrsrc.android.views.TitleBar;
import com.njrsrc.common.url.RequestUrlProvider;
import com.njrsrc.common.utils.Helpers;
import com.njrsrc.common.utils.MsgID;

public class HrLetterDetailActivity extends BaseSampleActivity implements OnClickListener{
	private TitleBar mBar;
	private RelativeLayout title;
	private TextView letterContent;
	private TextView letterTitle;
	private ProgressDialog progressDialog;
	private UserInfo userInfo;
	private HrLetterInfo letterInfo;
	private String hrLetterId;
	
	private Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case MsgID.HR_LETTER_DETAIL_QUERY_ERROR:
				Helpers.closeProgressDialog(progressDialog);
				Toast.makeText(HrLetterDetailActivity.this, 
						getResources().getString(R.string.connectError), 
						Toast.LENGTH_SHORT).show();
				break;
			case MsgID.HR_LETTER_DETAIL_QUERY_FAIL:
				Helpers.closeProgressDialog(progressDialog);
				break;
			case MsgID.HR_LETTER_DETAIL_QUERY_SUCCESS:
				Helpers.closeProgressDialog(progressDialog);
				mBar.setTitleText(letterInfo.getTitle());
				letterTitle.setText(letterInfo.getTitle());
				letterContent.setText(letterInfo.getBody());
				break;

			default:
				break;
			}
			super.handleMessage(msg);
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_hr_letter_detail);
		initView();
	}
	
	private void initView() {
		mBar = (TitleBar) findViewById(R.id.activity_title_bar);
		mBar.setTitleText("人士来信");
		mBar.back.setOnClickListener(this);
		
		title = (RelativeLayout) findViewById(R.id.job_detail_company_layout);
		letterTitle = (TextView) findViewById(R.id.hrLetterTitle);
		letterContent = (TextView) findViewById(R.id.letterContent);
		
		title.setOnClickListener(this);
		
		Bundle data = getIntent().getExtras();
		hrLetterId = data.getString("hrLetterId");
		initData();
	}
	
	private void initData()
	{
		progressDialog = Helpers.showProgressDialog(HrLetterDetailActivity.this, R.string.loading);
		List<String> params = new ArrayList<String>();
		params.add(AppApplication.mToken);
		params.add(AppApplication.mUserID);
		params.add(hrLetterId);
		String requestUrl = RequestUrlProvider.HR_LETTER_SHOW;
		String url = Helpers.combinaStr(requestUrl, params);
		userInfo = new UserInfo();
		letterInfo = new HrLetterInfo();
		Object[] args = new Object[4];
		args[0] = mHandler;
		args[1] = url;
		args[2] = userInfo;
		args[3] = letterInfo;
		new HrLetterDetailTask().execute(args);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tb_ibtn_back:
			finish();
			break;
		case R.id.job_detail_company_layout:
			Bundle companyData = new Bundle();
			companyData.putString("companyId", letterInfo.getCompany().getCompany_id());
			
			Intent companyPositionIntent = new Intent();
			companyPositionIntent.setClass(HrLetterDetailActivity.this, CompanyPositionActivity.class);
			companyPositionIntent.putExtras(companyData);
			startActivity(companyPositionIntent);
			break;
		default:
			break;
		}
	}

}
